package com.qiaofang.service.impl;

import com.google.common.collect.Lists;
import com.qiaofang.common.model.UiSelectBean;
import com.qiaofang.entity.Area;
import com.qiaofang.repository.AreaRepository;
import com.qiaofang.service.AreaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AreaServiceImpl implements AreaService {

    @Autowired
    private AreaRepository areaRepository;

    @Override
    public List<UiSelectBean> listProvince() {
        List<Area> areas = areaRepository.listProvince();
        List<UiSelectBean> list = listToSelectBean(areas);
        return list;
    }

    @Override
    public List<UiSelectBean> listCity(Long provinceId) {
        List<Area> areas = areaRepository.listCity(provinceId);
        List<UiSelectBean> list = listToSelectBean(areas);
        return list;
    }

    private List<UiSelectBean> listToSelectBean(List<Area> areas) {
        List<UiSelectBean> list = Lists.newArrayList();
        if (areas != null) {
            for (Area area : areas) {
                UiSelectBean uiSelectBean = new UiSelectBean();
                uiSelectBean.setValue(area.getId());
                uiSelectBean.setLabel(area.getName());
                list.add(uiSelectBean);
            }
        }
        return list;
    }

    @Override
    public List<UiSelectBean> listZone(Long cityId) {
        List<Area> areas = areaRepository.listZone(cityId);
        List<UiSelectBean> list = listToSelectBean(areas);
        return list;
    }
}
